package com.intretech.umsin.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intretech.umsin.common.web.ApiResult;
import com.intretech.umsin.demo.entity.UserEntity;
import com.intretech.umsin.demo.param.UserParam;
import com.intretech.umsin.demo.service.UserService;
import com.intretech.umsin.demo.vo.UserVo;
import com.intretech.umsin.demo.vo.converter.UserVoEntityConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @version V1.0
 * @ClassName: UserController
 * @Description: UserController 服务实现
 * @author: 李启联
 * @date: 2022-04-21 16:27
 * @Copyright: Copyright (c) 2022
 */
@RestController
@RequestMapping("/user")
@Api(tags = "model-demo")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserVoEntityConverter userVoEntityConverter;

    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询单个用户对象", notes = "查询单用户对象")
    public ApiResult<UserVo> queryUserById(@PathVariable String id){
        Function<String,UserVo> fun = p -> userVoEntityConverter.reverseFromTarget(userService.getById(p));
        return ApiResult.responseFor(fun,id);
    }

    @PostMapping("/listByIds")
    @ApiOperation(value = "根据ID列表查询用户集合对象", notes = "根据ID列表查询用户集合对象")
    public ApiResult<List<UserVo>> queryUserByIds(@RequestBody List<String> ids){
        Function<List<String>,List<UserVo>> fun = p -> userVoEntityConverter.reverseFromTarget(userService.listByIds(p));
        return ApiResult.responseFor(fun,ids);
    }

    @PostMapping("/page/{pageNo}/{pageSize}")
    @ApiOperation(value = "查询返回用户分页对象", notes = "查询返回用户分页对象")
    public ApiResult<IPage<UserVo>> queryForPage(@RequestBody UserParam params, @PathVariable Integer pageNo, @PathVariable Integer pageSize){
        BiFunction<IPage,UserParam,IPage<UserVo>> fun = (p1, p2) -> {
            IPage<UserEntity> page0 = userService.queryForPage(p1, p2);
            return page0.convert(e->userVoEntityConverter.reverseFromTarget(e));
        };
        return ApiResult.responseFor(fun,new Page(pageNo,pageSize), params);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询用户对象列表", notes = "查询返回用户对象列表")
    public ApiResult<List<UserVo>> queryForList(@RequestBody UserParam params){
        Function<UserParam,List<UserVo>> fun = p ->{
            List<UserEntity> ls = userService.queryForList(p);
            return userVoEntityConverter.reverseFromTarget(ls);
        };
        return ApiResult.responseFor(fun,params);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增用户对象", notes = "新增用户对象")
    public ApiResult<UserVo> add(@RequestBody UserVo vo){
        Function<UserVo,UserVo> fun = p -> {
            UserEntity entity = userVoEntityConverter.convertToTarget(p);
            return userVoEntityConverter.reverseFromTarget(userService.add(entity));
        };
        return ApiResult.responseFor(fun,vo);
    }

    @PostMapping("/adds")
    @ApiOperation(value = "新增用户对象列表", notes = "新增用户对象列表")
    public ApiResult<List<UserVo>> adds(@RequestBody List<UserVo> voList){
        Function<List<UserVo>,List<UserVo>> fun = p -> {
            List<UserEntity> entities = userVoEntityConverter.convertToTarget(p);
            return userVoEntityConverter.reverseFromTarget(userService.adds(entities));
        };
        return ApiResult.responseFor(fun,voList);
    }

    @PutMapping("/modify")
    @ApiOperation(value = "更新用户对象", notes = "更新用户对象")
    public ApiResult<UserVo> modify(@RequestBody UserVo vo){
        Function<UserVo,UserVo> fun = p->{
            UserEntity entity = userVoEntityConverter.convertToTarget(p);
            return userVoEntityConverter.reverseFromTarget(userService.modify(entity));
        };
        return ApiResult.responseFor(fun,vo);
    }

    @PutMapping("/modifys")
    @ApiOperation(value = "更新用户对象", notes = "更新用户对象")
    public ApiResult<List<UserVo>> modifys(@RequestBody List<UserVo> voList){
        Function<List<UserVo>,List<UserVo>> fun = p->{
            List<UserEntity> entities = userVoEntityConverter.convertToTarget(p);
            return userVoEntityConverter.reverseFromTarget(userService.modifys(entities));
        };
        return ApiResult.responseFor(fun,voList);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除单个用户对象", notes = "删除单个用户对象")
    public ApiResult<Void> removeUser(@PathVariable String id){
        Consumer<String> consumer = p -> userService.removeById(p);
        return ApiResult.responseFor(consumer,id);
    }

    @DeleteMapping("/removes")
    @ApiOperation(value = "批量删除用户对象", notes = "批量删除用户对象")
    public ApiResult<Void> removeUsers(@RequestBody List<String> ids){
        Consumer<List<String>> consumer = p -> userService.removeBatchByIds(p);
        return ApiResult.responseFor(consumer, ids);
    }



}
